import pandas as pd
from pyecharts import options as opts
from pyecharts.charts import Pie, Grid

df = pd.read_excel('../data/20250527爱奇艺风云榜电影榜res.xlsx')

typeTags = list(df['类型1']) + list(df['类型2'])
words_num = {}
for tag in typeTags:
    if tag not in words_num:
        words_num[tag] = 1
        continue
    words_num[tag] += 1

words_num = dict(sorted(words_num.items(), key=lambda item: item[1], reverse=True))
data_pie = [(key, value) for key, value in words_num.items()]  # 转换成元组方便pyecharts展示

pie = Pie().add(
    series_name="电影标签", data_pair=data_pie,
    radius=['10%', '60%'],
    # center=['80%', '65%'],
    rosetype="radius",
).set_global_opts(
    title_opts=opts.TitleOpts(
        title="爱奇艺风云榜电影类标签次数玫瑰图",
        subtitle="数据来源：爱奇艺风云榜",
        pos_left='center',
        pos_top='3%',
        # 设置标题样式
        title_textstyle_opts=opts.TextStyleOpts(
            font_size=20,
        ),
    ),
    legend_opts=opts.LegendOpts(pos_bottom='5%', pos_left='5%', pos_right='5%')
).set_series_opts(
    label_opts=opts.LabelOpts(formatter='{b}:{d}%')  # 设置显示格式为百分比
)

grid = Grid(  # 画布配置
    init_opts=opts.InitOpts(
        width="1300px",  # 图的大小
        height='750px'
    ))
grid.add(pie, grid_opts=opts.GridOpts(pos_top="13%")).render("../pic/爱奇艺风云榜电影类标签次数玫瑰图.html")
